#include <iostream>
#include <thread>
#include <signal.h>
#include <backtrace.h>
#include <execinfo.h>
#include "zuc/task/zucsvr.h"
#include "log/zuclog.h"
#include "log/datacollector.hh"
#include "config.h"
#include "hal/sensormgr/torq_sensor.hh"
#include "jkutil/threadpool.h"
#include "hwdrv/hwdrv.h"

using namespace std;
void backtraces_new(int sig)
{
    signal(sig, SIG_IGN);  // prevent multiple invocations on same signal
    printf("[JKZUC] Quit %d\n", sig);
    exit(-1);
}
ThreadPool* g_threadpool = nullptr;

int main(int argc, char** argv)
{
    signal(SIGINT, backtraces_new);
    signal(SIGTERM, backtraces_new);
    signal(SIGSEGV, backtraces_new);
    signal(SIGFPE, backtraces_new);
    signal(SIGUSR1, backtraces_new);

    zucLogInit();
    zuclog_info("MAIN", "Starting JKZUC " ZUC_VERSION_TAG);

    zucDiagInit();

    initZucSvr(argc, argv);  //在里面获取了cab类型

    std::thread zucsvrThread([]() {
        pthread_setname_np(pthread_self(), "zucsvr");
        runZucSvr();
    });
    zucsvrThread.detach();
    usleep(5e5);

    //硬件接口初始化
    extern int zuc_cab_type;
    extern int zuc_sim;

    hwdrv::HwMgr::get().init(zuc_cab_type, zuc_sim != 0 ? 1 : -1);

    //初始化线程池
    // ThreadPool threadpool(4);
    // g_threadpool = &threadpool;

    // 传感器线程
    initSensorSvr();
    std::thread sensorsvrThread([]() {
        pthread_setname_np(pthread_self(), "sensor_thread");
        runSensorSvr();
    });
    sensorsvrThread.detach();
    usleep(5e5);

    extern int zuctaskmain(int, char**);
    zuctaskmain(argc, argv);

    zucDiagDeInit();

    zuclog_info("MAIN", "Stop JKZUC");
    return 0;
}